// pages/clientEdit/clientEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    poolId:'',
    managerId:'',
    info:{},
    labelSelect:[],
    filter_show:false,
    popup_show:false,
    time_popup_show:false,
    currentTimestamp:'',
    selectOption:[],
    labelOption:[],
    show:true,
    nowSelect:'',//当前选项类型
    nature:'',//性质
    workFlow:'',//跟进状态
    source:'',//来源
    birthDay:'',
    level:'',
    industry:'',
    type:'',
    scale:'',
    followTime:'',
    staffSelect:[],
    isBack:false,
    commit_name:'',
    commit_name_id:''
  },

  chooseStaff(e){
    this.setData({
      staff_now:e.currentTarget.dataset.type
    })
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
/**选择时间 */
chooseTime(e){
  let currentTimestamp = currentTimestamp = this.data.followTime?new Date(this.data.followTime).getTime():new Date().getTime();
  this.setData({
    time_popup_show:true,
    currentTimestamp:currentTimestamp
  })
},
/**日期选择确认 */
timeConfirm(e){
  this.setData({
    followTime:api.formatDate('Y-m-d H:i:s',e.detail),
    time_popup_show:false
  })
},
timeCancel(e){
  this.setData({
    time_popup_show:false
  })
},

  /**删除标签 */
  del_label(e){
    let id = e.currentTarget.dataset.id;
    let arr = this.data.labelOption;
    let newArr = [];
    arr.forEach((v)=>{
     
      v.array.forEach((t,n)=>{
        if(t.id == id){
          t.selected = '';
        }
      })
      newArr.push(v)
    })
    let lableSelect = this.data.labelSelect.filter(item=>item.id!==id);
    this.setData({
      labelOption:newArr,
      labelSelect:lableSelect
    })
  },

  /**选择标签 */
  chooseTag(e){
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    let arr = [];
    let labelSelect=[];
    let selected = false;
    this.data.labelOption.forEach((v,i)=>{
      if(v.title==name){
        v.array.forEach((t,n)=>{
          selected = t.selected;
          v.array[n].selected = '';
          if(t.id==id){
            v.array[n].selected=(selected===true?'':true);
            if(v.array[n].selected){
              labelSelect.push({id:t.id,name:t.name})
            }
          }
        })
      }else{
        v.array.forEach((t,n)=>{
          if(v.array[n].selected){
            labelSelect.push({id:t.id,name:t.name})
          }
        })
      }
      arr.push(v);
    }); 
    this.setData({
      labelOption:arr,
      labelSelect:labelSelect
    })


  },
  /**弹出标签选择框 */
  showTag(){
    this.setData({
      filter_show:true,
    })
  },
  cancelFilter(){
    this.setData({
      filter_show:false
    })
  },
  /**重置 */
  resetTage(){
    let arr = [];
    this.data.labelOption.forEach((v)=>{
      v.array.forEach((value,n)=>{
        v.array[n].selected='';
      })
      arr.push(v)
    })
    this.setData({
      labelOption:arr,
      labelSelect:[],
    })
  },
  confirmFilter(){
    this.cancelFilter();
  },
  /**日期选择 */
  bindChange(e){
    this.setData({
      birthDay: e.detail.value
    })
  },
  
  /**选项取消 */
  onCancel(e){
    this.setData({
      popup_show:false
    })
  },
  /**选项切换 */
  onConfirm(event){
    const { picker, value, index } = event.detail;
     switch(this.data.nowSelect){
      case 'nature':
         //选择性质
         this.setData({
          nature:value,
          popup_show:false
        })
      break;
      case 'workFlow':
         //选择跟进状态
         this.setData({
          workFlow:value,
          popup_show:false
        })
      break;
      case 'level':
         //选择客户级别
         this.setData({
          level:value,
          popup_show:false
        })
      break;
      case 'scale':
         //选择人员规模
         this.setData({
          scale:value,
          popup_show:false
        })
      break;
      case 'type':
         //选择客户分类
         this.setData({
          type:value,
          popup_show:false
        })
      break;
      case 'industry':
         //选择所属行业
         this.setData({
          industry:value,
          popup_show:false
        })
      break;
      case 'source':
         //选择来源
         this.setData({
          source:value,
          popup_show:false
        })
      break;
      case 'pool':
        //选择来源
        this.setData({
         poolName:value.text,
         poolId:value.value,
         popup_show:false
       })
     break;
    }
  },
  showMore(){
    this.setData({
      show:this.data.show?false:true
    })
  },
  /**选项 */
  chooseSelect(e){
    let that = this;
    let type = e.currentTarget.dataset.type;
   
    api.curlPost('?cla=clientAppClass&fun=params',{type:type}).then((res)=>{
      that.setData({
        nowSelect:type,
        selectOption:res.data,
        popup_show:true
      })
    })
  },
  /**删除协作人 */
  delStaff(e){
    let id = e.currentTarget.dataset.id;
    let staffSelect = this.data.staffSelect.filter(item=>item.id!==id);
    this.setData({
      staffSelect:staffSelect
    })
  },

  commit(e){
    let para = e.detail.value;
    
    let labelMxId = [];
    this.data.labelSelect.forEach((v,i)=>{
      labelMxId.push(v.id)
    })
    para.tagId = labelMxId.join(',');

    let staffId = [];
    this.data.staffSelect.forEach((v,i)=>{
      staffId.push(v.id)
    })
    para.collaborator = staffId.join(',');
    para.khid=this.data.info.khid?this.data.info.khid:'',
    para.poolId = this.data.poolId,
    para.manager = this.data.managerId,
    api.curlPost('?cla=clientOtherApp&fun=editEdit',para).then((res)=>{
      // wx.redirectTo({
      //   url: '../clientInfo/clientInfo?id='+res.data.id
      // })
      wx.navigateBack({
        delta: 1,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let that = this;
    if(!api.isBlank(id)){
      api.curlPost('?cla=clientAppClass&fun=detail',{khid:id}).then((res)=>{
        that.setData({
          info:res.data.info,
          manage:res.data.manage,
          invoice:res.data.invoice,
          id:res.data.info.khid,
          followTime:res.data.manage.followTime,
          workFlow:res.data.info.workFlow,
          source:res.data.manage.source,
          scale:res.data.manage.scale,
          industry:res.data.manage.worindustrykFlow,
          type:res.data.manage.classify,
          level:res.data.manage.level,
          birthDay:res.data.info.birthDay,
          nature:res.data.info.nature,
          labelSelect:[],
          staffSelect:[],
        })
      })
    }
    api.curlPost('?cla=clientAppClass&fun=params',{type:'tag'}).then((res)=>{
      that.setData({
        labelOption:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.isBack && this.data.commit_name_id){
      if (this.data.staff_now =='colloabor') {
        let flag = false;
        let resultArr=[];
        this.data.staffSelect.forEach((item)=>{
          if(item.id == that.data.commit_name_id){
            flag = true;
          }
          resultArr.push(item);
        })
        if(!flag){
          resultArr.push({id:this.data.commit_name_id,name:this.data.commit_name})
        }
        this.setData({
          staffSelect:resultArr,
          isBack:false,
          commit_name:'',
          commit_name_id:''
        })
      }else if(this.data.staff_now =='manager'){
        this.setData({
          managerName:this.data.commit_name,
          managerId:this.data.commit_name_id,
          isBack:false,
          commit_name:'',
          commit_name_id:''
        })
      }
      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})